package com.excellent.system.controller;

import java.util.List;

import com.excellent.common.core.domain.entity.SysDictData;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.utils.ShiroUtils;
import com.excellent.system.service.ISysDictDataService;
import com.excellent.system.service.impl.SysTaskStuServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.excellent.common.annotation.Log;
import com.excellent.common.enums.BusinessType;
import com.excellent.system.domain.SysTaskStu;
import com.excellent.system.service.ISysTaskStuService;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * IT成绩录入Controller
 * 
 * @author ruoyi
 * @date 2022-05-12
 */
@Controller
@RequestMapping("/system/teaItTask")
public class SysTaskStuITController extends BaseController
{
    private String prefix = "system/teaItTask";

    @Autowired
    private ISysTaskStuService sysTaskStuService;

    @Autowired
    private ISysDictDataService sysDictDataService;   //数据字典数据

    @GetMapping()
    public String teaItTask()
    {
        return prefix + "/teaItTask";
    }

    /**
     * 查询IT成绩录入列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTaskStu sysTaskStu)
    {
        SysUser user = ShiroUtils.getSysUser();    //查询到自己登录状态和权限
        user.getUserName();   //获取登录名字
        SysDictData sysDictData = new SysDictData();
        sysDictData.setRemark(user.getRemark());//登录名字和班级字典备注相查找
        List<SysDictData> list1 = sysDictDataService.selectDictDataList(sysDictData);
        sysDictData = list1.get(0);   //找到获取到的字典数据
        sysTaskStu.setStuClass(sysDictData.getRemark());   //找到对应的班级进行判定
        startPage();
        List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
        return getDataTable(list);
    }

    /**
     * 导出IT成绩录入列表
     */
    @Log(title = "IT成绩录入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTaskStu sysTaskStu)
    {
        List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
        ExcelUtil<SysTaskStu> util = new ExcelUtil<SysTaskStu>(SysTaskStu.class);
        return util.exportExcel(list, "teaItTask");
    }

    /**
     * 新增IT成绩录入
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存IT成绩录入
     */
    @Log(title = "IT成绩录入", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.insertSysTaskStu(sysTaskStu));
    }

    /**
     * 修改IT成绩录入
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id);
        mmap.put("sysTaskStu", sysTaskStu);
        return prefix + "/edit";
    }

    /**
     * 修改保存IT成绩录入
     */
    @Log(title = "IT成绩录入", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.updateSysTaskStu(sysTaskStu));
    }

    /**
     * 删除IT成绩录入
     */
    @Log(title = "IT成绩录入", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTaskStuService.deleteSysTaskStuByIds(ids));
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysTaskStu> util = new ExcelUtil<SysTaskStu>(SysTaskStu.class);
        List<SysTaskStu> userList = util.importExcel(file.getInputStream());
        for (SysTaskStu sysTaskStu: userList
             ) {
            SysTaskStu sys1 = new SysTaskStu(); //设置一个新的
            sys1.setStuClass(sysTaskStu.getStuClass());
            sys1.setTaskClass(sysTaskStu.getTaskClass());
            sys1.setStuName(sysTaskStu.getStuName());
            List<SysTaskStu> stu = sysTaskStuService.selectSysTaskStuList(sys1); //通过特征找到对应
            SysTaskStu sys2 = stu.get(0);    //获取到对应的
            sys2.setScore(sysTaskStu.getScore());//修改成绩
            sysTaskStuService.updateSysTaskStu(sys2);
        }
        return AjaxResult.success(true);
    }

    /**
     * 行内编辑  个字段值修改
     */
    @PostMapping("/editableSave")
    @ResponseBody
    public AjaxResult editableSave(@Param("keyValue") Long keyValue, @Param("field") String field, @Param("oldValue") Integer oldValue, @Param("newValue") Integer newValue)
    {
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(keyValue);
        sysTaskStu.setScore(newValue);
        sysTaskStuService.updateSysTaskStu(sysTaskStu);
        return AjaxResult.success(true);//SUCCESS 成功 ；ERROR 失败；其他字符串，则直接弹出字符串内容
    }
}
